// index.js
// 获取应用实例
const app = getApp()
import {
	platform,
	position,
	hotel,
	user,
	wxapp,
	gourmet
} from '../../api/apiData'
var pageNum1 = 1; //页码
Page({
	data: {
		navList: [
			// {
			// 	url: "/pages/hotel/index",
			// 	src: "/images/grid-1.png",
			// 	name: "旅游民宿"
			// },
			// {
			// 	url: "/pages/travel/index",
			// 	src: "/images/grid-2.png",
			// 	name: "周边旅游"
			// },
			// {
			// 	url: "/pages/food/index",
			// 	src: "/images/grid-3.png",
			// 	name: "美食餐饮"
			// },
			// {
			// 	url: "/pages/car/index",
			// 	src: "/images/grid-4.png",
			// 	name: "在线打车"
			// },
			// {
			// 	url: "/pages/shopping/index",
			// 	src: "/images/grid-5.png",
			// 	name: "在线商城"
			// }
		],
		loadMoreText: '加载中...', //底部文字
		list: [],
		list2: [],
		list3: [],
		banner: '',
		bannerList: [],
		notice: '',
		page: 1,
		distanceMy: '',
		page1: 1,
		page2: 1,
		page3: 1,
	},

	onLoad() {
		position.content.lists({
			position_id: 5
		}, res => {
			if (res.code == 1) {
				this.setData({
					navList: res.data
				})

			} else {
				wx.showToast({
					title: res.msg,
					icon: 'error'
				})
			}
		})
		position.content.lists({
			position_id: 1
		}, res => {
			if (res.code == 1) {
				this.setData({
					bannerList: res.data
				})

			} else {
				wx.showToast({
					title: res.msg,
					icon: 'error'
				})
			}
		})
		position.content.detail({
			position_id: 2
		}, res => {
			if (res.code == 1) {
				this.setData({
					notice: res.data.description
				})
			} else {
				wx.showToast({
					title: res.msg,
					icon: 'error'
				})
			}
		})
		this.getList()
		this.getList2()
		this.getList3()
	},
	toUrl(e) {
		let url = e.currentTarget.dataset.path
		let img = e.currentTarget.dataset.img
		if (url != '') {
			wx.navigateTo({
				url: url,
			})
		}else{
			wx.previewImage({
				urls: [img],
				current:img
			})
		}

	},
	/**
	 * 授权登录
	 */
	getUserInfos(e) {
		wx.login({
			success(res) {
				console.log(res)
				let code = res.code
				wx.getUserInfo({
					success: (res) => {
						var encryptedData = res.encryptedData;
						var iv = res.iv
						var info = res.rawData
						var obj = {
							code: code,
							iv: iv,
							encryptedData: encryptedData
						}
						wxapp.login(obj, res => {
							if (res.code == 1) {
								wx.setStorage({
									key: "token",
									data: res.data.token
								})
							} else {
								wx.showToast({
									title: res.msg,
									icon: 'none'
								})
							}
						})
					},
					fail: res => {

					}
				})
			}
		})
	},
	toChange() {
		let page = this.data.page
		let list = this.data.list
		if (list.length > 1) {
			this.setData({
				page: page + 1
			})
		} else {
			this.setData({
				page: 1
			})
		}
		this.getList()
	},
	toChange2() {
		let page = this.data.page2
		let list = this.data.list2
		if (list.length > 1) {
			this.setData({
				page2: page + 1
			})
		} else {
			this.setData({
				page2: 1
			})
		}
		this.getList2()
	},
	toChange3() {
		let page = this.data.page3
		let list = this.data.list3
		if (list.length > 1) {
			this.setData({
				page3: page + 1
			})
		} else {
			this.setData({
				page3: 1
			})
		}
		this.getList3()
	},
	getList() {
		let that = this
		platform.scenic.lists({
			page: this.data.page,
			size: 6
		}, res => {
			if (res.code == 1) {
				if (res.data.list.length > 0) {
					that.setData({
						list: res.data.list
					})
				}

			}

		})
	},
	getList2() {
		let that = this
		hotel.lists({
			page: this.data.page2,
			size: 6
		}, res => {
			if (res.code == 1) {
				if (res.data.list.length > 0) {
					that.setData({
						list2: res.data.list
					})
				}

			}
		})
	},
	getList3() {
		let that = this
		gourmet.shop.lists({
			page: this.data.page3,
			size: 6
		}, res => {
			if (res.code == 1) {
				if (res.data.list.length > 0) {
					that.setData({
						list3: res.data.list
					})
				}

			}
		})
	},
	distance(la1, lo1, la2, lo2) {
		var La1 = (la1 * Math.PI) / 180.0
		var La2 = (la2 * Math.PI) / 180.0
		var La3 = La1 - La2
		var Lb3 = (lo1 * Math.PI) / 180.0 - (lo2 * Math.PI) / 180.0
		var s =
			2 *
			Math.asin(
				Math.sqrt(
					Math.pow(Math.sin(La3 / 2), 2) +
					Math.cos(La1) * Math.cos(La2) * Math.pow(Math.sin(Lb3 / 2), 2)
				)
			)
		s = s * 6378.137
		s = Math.round(s * 10000) / 10000
		s = s.toFixed(2)
		return s
	},
	onShow() {
		this.getInfo()
	},
	getInfo() {
		let token = wx.getStorageSync('token')
		if (token) {
			// 获取用户详情
			user.detailS({}, res => {
				if (res.code == 20003) {
					this.getUserInfos()
				}
			})
		} else {
			this.getUserInfos()
		}
	},
	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},
	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})